package net.atomarrow.bs.controller;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import net.atomarrow.bs.domain.TbAccount;
import net.atomarrow.bs.form.AccountBsForm;
import net.atomarrow.bs.service.AccountService;
import net.atomarrow.pr.service.PermissionService;
import net.atomarrow.render.Render;
import net.atomarrow.sy.annotation.UnCheck;
import net.atomarrow.sy.base.BaseController;

public class AccountController extends BaseController {
    private AccountService accountService;
    private PermissionService permissionService;

    public Render index() {
        return renderJsp("/webdemo/account/index");
    }

    public Render ajax() {
        AccountBsForm form = getForm(AccountBsForm.class);
        form.setKeywords(getParamString("sSearch"));
        List<TbAccount> accounts = accountService.getList(form);
        return renderDataTables(accounts, form.getDataTotal(), form.getDraw());
    }

    @UnCheck
    public Render login() {
        return renderJsp("/webdemo/account/login");
    }

    @UnCheck
    public Render testAction() {
        System.out.println("testAction");
        return renderAction("/account/testAction2?b=2");
    }

    @UnCheck
    public Render testAction2() {
        System.out.println("testAction2");
        return renderAction("/account/testAction3?c=3");
    }

    @UnCheck
    public Render testAction3() {
        System.out.println("3a:" + getParamString("a"));
        System.out.println("3b:" + getParamString("b"));
        System.out.println("3c:" + getParamString("c"));
        System.out.println("testAction3");
        return renderJsp("/webdemo/account/result");
    }

    @UnCheck
    public Render dologin() {
        permissionService.loadPermission(1);
        return renderAction("/account");
    }

    @UnCheck
    public Render test() {
        accountService.testMax();
        return renderJsp("/webdemo/account/login");
    }

    @UnCheck
    public Render list() {
        accountService.getAll();
        return renderJsp("/webdemo/account/login");
    }

    @UnCheck
    public Render testFtl() {
        setMsg("this is test ");
        return renderFtl("hello");
    }

    @UnCheck
    public Render testFtl2() {
        setMsg("this is test ");
        return renderFtl("/account/list");
    }

    @UnCheck
    public Render testImg() throws Exception {
        
        URL url = new URL("https://www.baidu.com/img/bd_logo1.png");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5 * 1000);
        InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据
        return renderImg(inStream);
    }
    @UnCheck
    public Render testModify(){
        accountService.doTestModify();
        return renderJsonSuccess();
    }

}
